﻿@{
    ViewBag.Title = "上传附件";
}
<form id="form1" class="layui-form">
    <div class="div-content">
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                @Html.RadioButton("attachType", 0, true, new { title = "本地附件",@lay_filter="raQT" })
                @Html.RadioButton("attachType", 1, false, new { title = "远程附件",@lay_filter="raQT" })
            </div>
        </div>
        <div class="dl-attach-box">
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    <input type="hidden" id="hidFilePath" class="upload-path" />
                    <input type="hidden" id="hidFileSize" class="upload-size" />
                    <input type="text" id="txtFileName" class="input txt upload-name" />
                    <div class="upload-box upload-attach"></div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    提示：上传文件后，可更改附件名称
                </div>
            </div>
        </div>
        <div class="dl-attach-box">
            <div class="layui-form-item">
                <label class="layui-form-label">附件名称</label>
                <div class="layui-input-block">
                    <input type="text" id="txtRemoteTitle" class="input txt" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">远程地址</label>
                <div class="layui-input-block">
                    <input type="text" id="txtRemoteUrl" class="input txt" /> <span>*以“http://”开头</span>
                </div>
            </div>
        </div>
    </div>
</form>
@section Scripts{
    <script type="text/javascript" src="/scripts/libs/js/uploader.js"></script>
    <script type="text/javascript">
        var api = parent.layer.getFrameIndex(window.name); //获取父窗体对象
        layui.use(['form', 'element'], function () {
            var form = layui.form, $ = layui.jquery;

             form.on('radio(raQT)', function (data) {
                changeQT(data.value);
            });
            window.changeQT = function (value, sel) {
                if (value == 0) {
                    $(".dl-attach-box").hide();
                    $(".dl-attach-box").eq(0).show();
                } else if (value == 1) {
                    $(".dl-attach-box").hide();
                    $(".dl-attach-box").eq(1).show();
                }
                $("input[name=attachType][value='" + value + "']").attr('checked', 'true');
                form.render("radio");
            }

            //初始化上传控年
            $(".upload-attach").InitUploader({ sendurl: "/upload_ajax/action", swf: "/scripts/libs/module/uploader/uploader.swf", type: 1, chunked: true });
        });
        function submitValue(data) {
            //修改状态，赋值给表单
            if ($(data).length > 0) {
                var parentObj = $(data).parent();
                var filePath = parentObj.find("input[name='hid_attach_filepath']").val();
                var fileName = parentObj.find("input[name='hid_attach_filename']").val();
                var fileSize = parentObj.find("input[name='hid_attach_filesize']").val() * 1024; //转换为字节
                if (filePath.toLowerCase().startsWith("http")) {
                    changeQT(1);
                    $("#txtRemoteTitle").val(fileName);
                    $("#txtRemoteUrl").val(filePath);
                    $(".dl-attach-box").hide();
                    $(".dl-attach-box").eq(1).show();
                } else {
                    changeQT(0);
                    $("#txtFileName").val(fileName);
                    $("#hidFilePath").val(filePath);
                    $("#hidFileSize").val(fileSize);
                    $(".dl-attach-box").hide();
                    $(".dl-attach-box").eq(0).show();
                }
            } else {
                changeQT(0);
            }
        }

        //创建附件节点
        function execAttachHtml(data) {
            var currDocument = $(document); //当前文档
            if ($("input[name='attachType']:checked").val() == 0) {
                if ($("#hidFilePath").val() == "" || $("#hidFileSize").val() == "" || $("#txtFileName").val() == "") {
                    top.layer.alert('没有找到已上传附件，请上传！', {
                        closeBtn: 0
                    }, function (index) {
                        top.layer.close(index);
                    });
                    return false;
                }
                var fileExt = $("#hidFilePath").val().substring($("#hidFilePath").val().lastIndexOf(".") + 1).toUpperCase();
                var fileSize = Math.round($("#hidFileSize").val() / 1024);
                var fileSizeStr = fileSize + "KB";
                if (fileSize >= 1024) {
                    fileSizeStr = ForDight((fileSize / 1024), 1) + "MB";
                }
                return appendAttachHtml(data, $("#txtFileName").val(), $("#hidFilePath").val(), fileExt, fileSize, fileSizeStr); //插件节点
            } else {
                if ($("#txtRemoteTitle").val() == "" || $("#txtRemoteUrl").val() == "") {
                    top.layer.alert('远程附件地址或文件名为空！', {
                        closeBtn: 0
                    }, function (index) {
                        $("#txtRemoteTitle", currDocument).focus();
                        top.layer.close(index);
                    });
                    return false;
                }
                //获得远程文件信息
                $.ajax({
                    type: "post",
                    url: "/admin_ajax/get_remote_fileinfo",
                    data: {
                        "remotepath": $("#txtRemoteUrl").val()
                    },
                    dataType: "json",
                    success: function (dataJson, textStatus) {
                        if (dataJson.status == '0') {
                            top.layer.alert(dataJson.msg, {
                                closeBtn: 0
                            }, function (index) {
                                top.layer.close(index);
                            });
                            return false;
                        } else {
                            var fileSize = Math.round(dataJson.size / 1024);
                            var fileSizeStr = fileSize + "KB";
                            if (fileSize >= 1024) {
                                fileSizeStr = ForDight((fileSize / 1024), 1) + "MB";
                            }
                            return appendAttachHtml(data, $("#txtRemoteTitle").val(), $("#txtRemoteUrl").val(), dataJson.ext, fileSize, fileSizeStr); //插件节点
                        }
                    }
                });
            }
        }

        //创建附件节点的HTML
        function appendAttachHtml(data, fileName, filePath, fileExt, fileSize, fileSizeStr) {
            if ($(data).length > 0) {
                var parentObj = $(data).parent();
                parentObj.find("input[name='hid_attach_filename']").val(fileName);
                parentObj.find("input[name='hid_attach_filepath']").val(filePath);
                parentObj.find("input[name='hid_attach_fileSize']").val(fileSize);
                parentObj.find(".title").text(fileName);
                parentObj.find(".info .ext").text(fileExt);
                parentObj.find(".info .size").text(fileSizeStr);
            } else {
                var liHtml = '<div class="attach-li">'
                    + '<input name="hid_attach_id" type="hidden" value="0" />'
                    + '<input name="hid_attach_filename" type="hidden" value="' + fileName + '" />'
                    + '<input name="hid_attach_filepath" type="hidden" value="' + filePath + '" />'
                    + '<input name="hid_attach_filesize" type="hidden" value="' + fileSize + '" />'
                    + '<i class="iconfont icon-attachment"></i>'
                    + '<a href="javascript:;" onclick="delAttachNode(this);" class="del" title="删除附件"><i class="iconfont icon-remove"></i></a>'
                    + '<a href="javascript:;" onclick="showAttachDialog(this);" class="edit" title="更新附件"><i class="iconfont icon-pencil1"></i></a>'
                    + '<div class="title">' + fileName + '</div>'
                    + '<div class="info">类型：<span class="ext">' + fileExt + '</span> 大小：<span class="size">' + fileSizeStr + '</span> 下载：<span class="down">0</span>次</div>'
                    + '<div class="btns">下载积分：<input type="text" name="txt_attach_point" onkeydown="return checkNumber(event);" value="0" /></div>'
                    + '</div>';
                parent.$("#showAttachList").children(".attach-ul").append(liHtml);
            }
            parent.layer.close(api);
        }
    </script>
}